package com.cyx.system.manager;

import cn.hutool.core.bean.BeanUtil;
import com.cyx.common.core.constants.CacheConstants;
import com.cyx.common.redis.service.RedisService;
import com.cyx.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {

    @Autowired
    private RedisService redisService;

    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        // 去除无效字段
        Exam addExam = new Exam();
        BeanUtil.copyProperties(exam, addExam);
        addExam.setStatus(null);
        addExam.setCreateBy(null);
        addExam.setCreateTime(null);
        redisService.setCacheObject(getDetailKey(exam.getExamId()), addExam);
    }

    public void deleteCache(Long examId) {
        redisService.removeForList(getExamListKey(), examId);
        redisService.deleteObject(getDetailKey(examId));
        redisService.deleteObject(getExamQuestionListKey(examId));
    }

    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getExamQuestionListKey(Long examId) {
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }
}
